﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopOnline.BLL;

namespace WebUI
{
    public partial class BusinessCardPaper : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            lstPapers.OnNeedDataSource = lstPapers_OnNeedDataSource;
            lstPrice.OnNeedDataSource = lstPrice_OnNeedDataSource;
            lstPapers.OnClientClick = "loadByPaperID();";
            if(!IsPostBack)
                lstPapers.Refresh();
        }

        protected void btnPaperID_OnClick(object sender, EventArgs e)
        {
            lstPrice.Refresh();
            divPrice.Visible = true;
        }

        protected void lstPapers_OnNeedDataSource(object sender, EventArgs e)
        {
            CardPapers paper = new CardPapers();
            int count = paper.Search("Deleted = 0", "ID desc", lstPapers.CurrentPageIndex * lstPapers.PageSize, lstPapers.PageSize);
            lstPapers.VirtualItemCount = count;
            lstPapers.DataSource = paper.DefaultView;
        }

        protected void lstPrice_OnNeedDataSource(object sender, EventArgs e)
        {
            if (hidPaperID.Value == "")
                hidPaperID.Value = "-1";
            PaperPrices paper = new PaperPrices();
            int count = paper.Search("PaperID = " + hidPaperID.Value, "Quantity asc", lstPrice.CurrentPageIndex * lstPrice.PageSize, lstPrice.PageSize);
            lstPrice.VirtualItemCount = count;
            lstPrice.DataSource = paper.DefaultView;
        }

        protected void btnAddPaper_OnClick(object sender, EventArgs e)
        {
            lstPapers.Refresh();
        }

        protected void btnRemovePaper_OnClick(object sender, EventArgs e)
        {
            lstPapers.DeleteCheckedItems();
            lstPapers.Refresh();
        }

        public string GetPaperEditPageLink()
        {
            return this.ResolveUrl("~/EditPages/EditPaper.aspx?");
        }

        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            PaperPrices pp = new PaperPrices();
            pp.AddNew();
            pp.s_PaperID = hidPaperID.Value;
            pp.s_Quantity = txtQuantity.Text;
            pp.s_UnitPrice = txtUnitPrice.Text;
            pp.Save();
            lstPrice.Refresh();
            txtUnitPrice.Text = "";
            txtQuantity.Text = "";
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            lstPrice.DeleteCheckedItems();
            lstPrice.Refresh();
        }
    }
}